package com.hyou.demos.rabbitmq;

import java.util.Date;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import com.hyou.demos.rabbitmq.send.Sender;

@SpringBootApplication(scanBasePackages = { "com.hyou.demos.rabbitmq" })
public class ApplicationRabbitMQ {
    
    private static final Logger log = LoggerFactory.getLogger(ApplicationRabbitMQ.class);
    
    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(ApplicationRabbitMQ.class, args);
        
        final Sender sender = ctx.getBean("sender", Sender.class);
        
        new Thread(new Runnable() {
            
            @Override
            public void run() {
                while(true) {
                    try {
                        
                        String content = DateFormatUtils.format(new Date(), "HH:mm:ss");
                        log.debug("send msg to queue : {}", content);
                        sender.sendMsg(content);
                        
                        Thread.sleep(5000);
                    } catch(InterruptedException e) {
                        log.error(e.getMessage(), e);
                    }
                }
            }
        }).start();
    }
    
}
